//
//  ConvenientVC.m
//  hnGovernment
//
//  Created by 安仔 on 17/2/24.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "ConvenientVC.h"
#import "PublicServerCell.h"
#import "ConvenientReusableView.h"
#import "DTWebViewController.h"
#import "DTExhibitionModel.h"
#import "SserviceByCategoryObj.h"
@interface ConvenientVC ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *myCollection;
@property (nonatomic,strong) NSArray *enhibitionModelArray;// 表格展台模型数据

@end

@implementation ConvenientVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];    
    self.view.backgroundColor = RGB(237, 237, 237);
    [self requestSelectServiceByExhibitionCategoryId];
    [self createConvenientView];
}
//根据展台分类查询应用
- (void) requestSelectServiceByExhibitionCategoryId {
    __weak __typeof(self) weakSelf = self;
    [DDNetWorkManager exhibitionCategoryId:@"2" cityId:@"330102" controller:weakSelf success:^(id result, DDcacheType cacheType, DDresultType resultType) {
        int success = [result[@"success"] intValue];
        if (success == 1) {
            NSLog(@"-------%@",result);
            _enhibitionModelArray = [DTExhibitionModel objectArrayWithKeyValuesArray:result[@"data"]];
            [self.myCollection reloadData];
        }else {
            NSString *errorString = result[@"errorDetail"];
            [SVProgressHUD showErrorWithStatus:errorString];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:NSLocalString(@"网络错误")];
    }];
}

#pragma mark ---- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DTExhibitionModel *model = self.enhibitionModelArray[indexPath.section];
    NSArray *cellArray = model.exhibitionService;
    SserviceByCategoryObj *obj = cellArray[indexPath.item];
    DTWebViewController *webViewController = [[DTWebViewController alloc]init];
    webViewController.webViewUrlString = obj.url;
    webViewController.title = obj.serviceName;
    [self.navigationController pushViewController:webViewController animated:YES];
}

#pragma mark ---- 初始化网格
- (void)createConvenientView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; // 自定义的布局对象
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    self.myCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 10*m6Scale, KScreenWidth, self.view.bounds.size.height-140*m6Scale) collectionViewLayout:layout];
    self.myCollection.backgroundColor = [UIColor whiteColor];
    self.myCollection.dataSource = self;
    self.myCollection.delegate = self;
    [self.view addSubview:self.myCollection];
    //注册网格cell
    [self.myCollection registerClass:[PublicServerCell class] forCellWithReuseIdentifier:@"convenientCell"];
    [self.myCollection registerClass:[ConvenientReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerId"];
    [self.myCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerId"];
}

#pragma mark ---- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _enhibitionModelArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    DTExhibitionModel *obj = _enhibitionModelArray[section];
    return obj.exhibitionService.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PublicServerCell *cell = [self.myCollection dequeueReusableCellWithReuseIdentifier:@"convenientCell" forIndexPath:indexPath];
    //重写cell布局
    [cell.signImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(cell.centerX);
        make.top.mas_equalTo(20 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(64 * m6Scale, 64 * m6Scale));
    }];
    [cell.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(cell.centerX);
        make.top.mas_equalTo(cell.signImg.mas_bottom).offset(10 * m6Scale);
    }];
    
    DTExhibitionModel *model = self.enhibitionModelArray[indexPath.section];
    NSArray *cellArray = model.exhibitionService;
    SserviceByCategoryObj *obj = cellArray[indexPath.item];
    cell.titleLab.text = obj.serviceName;
    [cell.signImg sd_setImageWithURL:[NSURL URLWithString:obj.serviceImg]];
    return cell;
}

//网格的头.尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        ConvenientReusableView *headerView = [self.myCollection dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerId" forIndexPath:indexPath];
        if (headerView == nil) {
            headerView = [[ConvenientReusableView alloc] init];
        }
        //重写title的位置
        [headerView.titleLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(headerView.mas_left).offset(26*m6Scale);
            make.centerY.mas_equalTo(headerView.mas_centerY);
        }];
        DTExhibitionModel *obj = _enhibitionModelArray[indexPath.section];
        headerView.titleLab.text = obj.exhibitionName;
        headerView.lineImg.image = nil;
        return headerView;
    }
    else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        UICollectionReusableView *footerView = [self.myCollection dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerId" forIndexPath:indexPath];
        if (footerView == nil) {
            footerView = [[UICollectionReusableView alloc] init];
        }
        UIView *smallView = [[UIView alloc] init];
        smallView.backgroundColor = RGB(237, 237, 237);
        [footerView addSubview:smallView];
        [smallView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(footerView.mas_left);
            make.bottom.mas_equalTo(footerView.mas_bottom);
            make.size.mas_equalTo(CGSizeMake(KScreenWidth, 20*m6Scale));
        }];
        return footerView;
    }
    return nil;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(KScreenWidth/4, 144*m6Scale);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section  {
    return CGSizeMake(KScreenWidth, 72*m6Scale);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    CGSize size={KScreenWidth, 60*m6Scale};
    return size;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
